var orm = require("orm");
var express_orm = orm.express(require("./config/mysqlconfig.js"), {
    define: function(db, models) {
        for (var tablename in tables) {
            (function(tablename) {
                var table_info = tables[tablename];
                var id;
                for (var key in table_info) {
                    if (!id) {
                        key.indexOf("_id") !== -1 && key.split("-").length === 2;
                        id = key;
                    }
                    table_info[key] = typeMap[table_info[key]];
                }
                console.log("ORM定义初始化定义表：", tablename, "\t\t主键：", id);
                models[tablename] = db.define(tablename, table_info, {
                    id: id
                }, {
                    autoFetch: true, // global eager load
                    cache: false
                });
                // console.log(models[tablename]);
            }(tablename));
        }
    }
});

var typeMap = {
    "tinyint": Boolean,
    "int": Number,
    "float": Number,
    "double": Number,
    "varchar": String,
    "text": String,
    "char": String,
    "boolean": Boolean,
};

var tables = {
    "store_map": {
        "geotable_id": "int",
        "title": "varchar",
        "address": "varchar",
        "longitude": "double",
        "latitude": "double",
        "logo_url": "varchar",
        "phone": "varchar",
        "mobile": "varchar",
        "create_time": "varchar",
        "update_time": "varchar",
        "tags": "varchar",
        "description": "varchar",
        "store_imgs": "varchar"
    },
    "wechat_client_service": {
        "id": "varchar",
        "wechat_id": "varchar",
        "wechat_service_img": "varchar",
        "phone": "varchar"
    }

};
module.exports = express_orm;
